package com.code.lightning.generator;

import com.code.lightning.common.exception.LightingGeneratorException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Map;

/**
 * 模板生成
 * @author LGX_TvT
 * @date 2021-01-17 23:25
 */
@Slf4j
public class BasicTemplateGenerator implements TemplateGenerator {

    /**
     * 根据模板字符串生成内容
     * @param template 模板
     * @param model 模型
     * @return 生成内容
     */
    @Override
    public String generatorContent(String template, Map model) throws LightingGeneratorException {
        try {
            StringWriter result = new StringWriter();
            Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
            configuration.setDefaultEncoding("UTF-8");
            Template t = new Template("lightingTemplate", new StringReader(template), configuration);
            t.setEncoding("UTF-8");
            t.process(model, result);
            return result.toString();
        } catch (Exception e) {
            log.error("GeneratorContent fail: {}", e.getMessage());
            throw new LightingGeneratorException("GeneratorContent fail:" + e.getMessage());
        }
    }

    @Override
    public String generatorContent(File source, Map model) throws LightingGeneratorException {
        try {
            StringWriter result = new StringWriter();
            Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
            configuration.setDefaultEncoding("UTF-8");
            Template t = new Template("lightingTemplate", new FileReader(source), configuration);
            t.setEncoding("UTF-8");
            t.process(model, result);
            return result.toString();
        } catch (Exception e) {
            log.error("GeneratorContent fail: {}", e.getMessage());
            throw new LightingGeneratorException("GeneratorContent fail:" + e.getMessage());
        }
    }

    @Override
    public void generatorContent(String template, File target, Map model) throws LightingGeneratorException {
        try {
            FileWriter fileWriter = new FileWriter(target);
            Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
            configuration.setDefaultEncoding("UTF-8");
            Template t = new Template("lightingTemplate", new StringReader(template), configuration);
            t.setEncoding("UTF-8");
            t.process(model, fileWriter);
        } catch (Exception e) {
            log.error("GeneratorContent fail: {}", e.getMessage());
            throw new LightingGeneratorException("GeneratorContent fail:" + e.getMessage());
        }
    }

    @Override
    public void generatorContent(File source, File target, Map model) throws LightingGeneratorException {
        try {
            FileWriter fileWriter = new FileWriter(target);
            Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
            configuration.setDefaultEncoding("UTF-8");
            Template t = new Template("lightingTemplate", new FileReader(source), configuration);
            t.setEncoding("UTF-8");
            t.process(model, fileWriter);
        } catch (Exception e) {
            log.error("GeneratorContent fail: {}", e.getMessage());
            throw new LightingGeneratorException("GeneratorContent fail:" + e.getMessage());
        }
    }
}
